<template>
  <div class="linkShowSearchCondition">
    <el-row :gutter="10">
      <el-col :span="6" style="width:350px;">
        <div :class="lang === 'zh'?'showZh':'showEn'">{{$t('IP类型：')}}</div>
        <el-select v-model="record.ip_type" ref="ipType" :placeholder="$t('IP类型')" size='small' filterable style="margin-bottom:10px;">
          <el-option v-for="(item, key) in typeList" :key="key" :label="item" :value="key">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="6" style="width:350px;">
        <div :class="lang === 'zh'?'showZh':'showEn'">{{$t('数据方向：')}}</div>
        <el-select v-model="record.direction" ref="direction" :placeholder="$t('数据方向')" size='small' filterable style="margin-bottom:10px;">
          <el-option v-for="(item, key) in directionList" :key="key" :label="item" :value="key">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="6" style="width:350px;">
        <div :class="lang === 'zh'?'showZh':'showEn'">{{$t('源IP：')}}</div>
        <el-input v-emoji v-model="record.src_ip" ref="srcIp" style="width:217px;margin-bottom:10px;" size='small' :placeholder="$t('源IP')"></el-input>
      </el-col>
      <el-col :span="6" style="width:350px;">
        <div :class="lang === 'zh'?'showZh':'showEn'">{{$t('目的IP：')}}</div>
        <el-input v-emoji v-model="record.dst_ip" ref="dstIp" style="width:217px;margin-bottom:10px;" size='small' :placeholder="$t('目的IP')"></el-input>
      </el-col>
      <el-col :span="6" style="width:350px;">
        <div :class="lang === 'zh'?'showZh':'showEn'">{{$t('协议类型：')}}</div>
        <el-select v-model="record.protocol" ref='protocol' :placeholder="$t('协议类型')" size='small' filterable clearable style="margin-bottom:10px;">
          <el-option v-for="item in protocolList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="6" style="width:350px;">
        <div :class="lang === 'zh'?'showZh':'showEn'">{{$t('源端口：')}}</div>
        <el-input v-emoji v-model="record.sport" ref='sport' style="width:217px;margin-bottom:10px;" :disabled='record.protocol !== "udp" && record.protocol !== "tcp"' size='small' :placeholder="$t('源端口')"></el-input>
      </el-col>
      <el-col :span="6" style="width:350px;">
        <div :class="lang === 'zh'?'showZh':'showEn'">{{$t('目的端口：')}}</div>
        <el-input v-emoji v-model="record.dport" ref="dport" style="width:217px;margin-bottom:10px;" :disabled='record.protocol !== "udp" && record.protocol !== "tcp"' size='small' :placeholder="$t('目的端口')"></el-input>
      </el-col>
    </el-row>
    <div>
      <el-button type="success" size='small' @click="listData" class="finds">{{$t('查询')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
      <el-button type="danger" size='small' @click="clearData" class="clearData">{{$t('清空连接')}}</el-button>
      <span style="float:right;height:32px;display: flex;justify-content: center;align-items: center;">{{$t('当前总连接数：') + linkNum + $t('个')}}</span>
    </div>
    <el-table size='mini' ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%;margin-top:10px;">
      <el-table-column prop="ip_type" :label="$t('IP类型')" align='center' width="100"></el-table-column>
      <el-table-column prop="trans_type" :label="$t('协议类型')" align='center' width="100"></el-table-column>
      <el-table-column prop="timeout" :label="$t('超时时间')" align='center'></el-table-column>
      <el-table-column prop="state" :label="$t('状态')" align='center' min-width="105">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.state" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="flag" :label="$t('标志')" align='center' min-width="100">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.flag" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="forward" :label="$t('请求方向')" align='center'>
        <el-table-column prop="ip_src" :label="$t('源IP')" align='center' min-width="120">
          <template slot-scope="scope">
            <tooltip :copyText="scope.row.ip_src" class="wid190">
            </tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="sport" :label="$t('源端口')" align='center' min-width="110"></el-table-column>
        <el-table-column prop="ip_dst" :label="$t('目的IP')" align='center' min-width="120">
          <template slot-scope="scope">
            <tooltip :copyText="scope.row.ip_dst" class="wid190">
            </tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="dport" :label="$t('目的端口')" align='center' min-width="110"></el-table-column>
      </el-table-column>
      <el-table-column prop="forward" :label="$t('应答方向')" align='center'>
        <el-table-column prop="ip_rsc" :label="$t('源IP')" align='center' min-width="120">
          <template slot-scope="scope">
            <tooltip :copyText="scope.row.ip_rsc" class="wid190">
            </tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="rsport" :label="$t('源端口')" align='center' min-width="110"></el-table-column>
        <el-table-column prop="ip_rdt" :label="$t('目的IP')" align='center' min-width="120">
          <template slot-scope="scope">
            <tooltip :copyText="scope.row.ip_rdt" class="wid190">
            </tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="rdport" :label="$t('目的端口')" align='center' min-width="110"></el-table-column>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="record.page" :page-size="record.limit" :page-sizes="[10, 20, 30, 40, 50]" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <del-show-dialog v-if="delshowDialogVisible" ref="refDelshowDialogVisible"></del-show-dialog>
  </div>
</template>
<script>
import { getArray, clearArray, getAllCount, delArray } from '@/api/network/link/show'
import DelShowDialog from './components/DelShowDialog.vue'
import { inputClass, selectClass } from '@/utils/input-class.js'

export default {
  components: { DelShowDialog },
  name: 'LinkShow',
  data () {
    return {
      delshowDialogVisible: false,
      tableData: [],
      record: {
        ip_type: 'ipv4',
        direction: 'request',
        protocol: '',
        src_ip: '',
        dst_ip: '',
        sport: '',
        dport: '',
        page: 1,
        limit: 20
      },
      linkNum: 0,
      total: 0,
      typeList: {
        'ipv4': 'IPv4',
        'ipv6': 'IPv6'
      },
      lang: this.$i18n.locale,
      directionList: {
        'request': this.$t('请求方向'),
        'reply': this.$t('应答方向')
      },
      protocolList: ['udp', 'tcp', 'icmp', 'icmpv6']
    }
  },
  created () {
    this.listData()
    getAllCount({ ip_type: this.record.ip_type, direction: this.record.direction }).then(res => {
      if (res.status === 0) {
        this.linkNum = res.info[0].num
      }
    })
    this.timer = setInterval(() => {
      getAllCount({ ip_type: this.record.ip_type, direction: this.record.direction }).then(res => {
        if (res.status === 0) {
          this.linkNum = res.info[0].num
        }
      }).catch(e => {
        clearInterval(this.timer)
      })
    }, 5000)
  },
  beforeDestroy () {
    if (this.timer) {
      clearInterval(this.timer) // 在Vue实例销毁前，清除我们的定时器
    }
  },
  mounted () {
    this.$nextTick(() => {
      const data = ['srcIp', 'dstIp', 'sport', 'dport']
      const selectData = ['ipType', 'direction', 'protocol']
      inputClass(this.$refs, data)
      selectClass(this.$refs, selectData)
    })
  },
  methods: {
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.record.limit = val
      this.record.page = 1
      this.listData()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.record.page = val
      this.listData()
    },
    // 列表
    listData () {
      let ipv4 = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      let ipv6 = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      if (this.record.src_ip && this.record.ip_type === 'ipv4' && !ipv4.test(this.record.src_ip)) {
        this.$messages.error(this.$t('源IP不是ipv4格式地址'))
      } else if (this.record.src_ip && this.record.ip_type === 'ipv6' && !ipv6.test(this.record.src_ip)) {
        this.$messages.error(this.$t('源IP不是ipv6格式地址'))
      } else if (this.record.dst_ip && this.record.ip_type === 'ipv4' && !ipv4.test(this.record.dst_ip)) {
        this.$messages.error(this.$t('目的IP不是ipv4格式地址'))
      } else if (this.record.dst_ip && this.record.ip_type === 'ipv6' && !ipv6.test(this.record.dst_ip)) {
        this.$messages.error(this.$t('目的IP不是ipv6格式地址'))
      } else if (this.record.sport && (Number(this.record.sport) > 65535 || Number(this.record.sport) < 0 || isNaN(Number(this.record.sport)))) {
        this.$messages.error(this.$t('源端口的范围为0~65535'))
      } else if (this.record.dport && (Number(this.record.dport) > 65535 || Number(this.record.dport) < 0 || isNaN(Number(this.record.dport)))) {
        this.$messages.error(this.$t('目的端口的范围为0~65535'))
      } else if ((this.record.sport && !this.isInteger(this.record.sport)) || (this.record.dport && !this.isInteger(this.record.dport))) {
        this.$messages.error(this.$t('端口请输入整数'))
      } else {
        getArray(this.record).then(res => {
          if (res.status === 0) {
            this.tableData = res.info
            this.total = res.count
          }
        })
      }
    },
    clearData () {
      this.$confirm(this.$t('您是否要清空所有数据？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        clearArray().then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t('清空成功'),
              type: 'success'
            })
            this.record = {
              ip_type: 'ipv4',
              direction: 'request',
              protocol: '',
              src_ip: '',
              dst_ip: '',
              sport: '',
              dport: '',
              page: 1,
              limit: 20
            }
            this.listData()
          }
        })
      })
    },
    deleteData () {
      this.$confirm(this.$t('您是否要删除所有查询到的数据？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        let ipv4 = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
        let ipv6 = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
        if (this.record.src_ip && this.record.ip_type === 'ipv4' && !ipv4.test(this.record.src_ip)) {
          this.$messages.error(this.$t('源IP不是ipv4格式地址'))
        } else if (this.record.src_ip && this.record.ip_type === 'ipv6' && !ipv6.test(this.record.src_ip)) {
          this.$messages.error(this.$t('源IP不是ipv6格式地址'))
        } else if (this.record.dst_ip && this.record.ip_type === 'ipv4' && !ipv4.test(this.record.dst_ip)) {
          this.$messages.error(this.$t('目的IP不是ipv4格式地址'))
        } else if (this.record.dst_ip && this.record.ip_type === 'ipv6' && !ipv6.test(this.record.dst_ip)) {
          this.$messages.error(this.$t('目的IP不是ipv6格式地址'))
        } else if (this.record.sport && (Number(this.record.sport) > 65535 || Number(this.record.sport) < 0 || isNaN(Number(this.record.sport)))) {
          this.$messages.error(this.$t('源端口的范围为0~65535'))
        } else if (this.record.dport && (Number(this.record.dport) > 65535 || Number(this.record.dport) < 0 || isNaN(Number(this.record.dport)))) {
          this.$messages.error(this.$t('目的端口的范围为0~65535'))
        } else if ((this.record.sport && !this.isInteger(this.record.sport)) || (this.record.dport && !this.isInteger(this.record.dport))) {
          this.$messages.error(this.$t('端口请输入整数'))
        } else {
          delArray(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.record.page = 1
              this.listData()
            }
          })
        }
      })

      // this.delshowDialogVisible = true
      // this.$nextTick(() => {
      //   this.$refs.refDelshowDialogVisible.init()
      // })
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
<style lang="scss" scoped>
.showZh {
  width: 70px;
  display: inline-block;
}
.showEn {
  width: 105px;
  display: inline-block;
}
</style>
